package com.example.finalproject.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Rect;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.finalproject.R;
import com.example.finalproject.adapter.WallpaperAdapter;

import java.util.Arrays;
import java.util.List;

public class WallpaperFragment extends Fragment {

    private RecyclerView recyclerView;
    private WallpaperAdapter adapter;
    private List<String> wallpaperNames = Arrays.asList(
            "wallpaper_waterfall", "wallpaper_starry", "wallpaper_rock",
            "wallpaper_beach", "wallpaper_forest", "wallpaper_lake",
            "wallpaper_road","wallpaper_sea","wallpaper_snowfield",
            "wallpaper_sunset");

    public WallpaperFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_wallpaper, container, false);
        recyclerView = root.findViewById(R.id.wallpaperRecyclerView);

        int spanCount = 2; // 两列
        StaggeredGridLayoutManager layoutManager =
                new StaggeredGridLayoutManager(spanCount, StaggeredGridLayoutManager.VERTICAL);
        layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
        recyclerView.setLayoutManager(layoutManager);

        adapter = new WallpaperAdapter(getContext(), wallpaperNames, this::onWallpaperSelected);
        recyclerView.setAdapter(adapter);

        // 为了实现均匀间距建议再添加 ItemDecoration
        int totalSpace = getResources().getDimensionPixelSize(R.dimen.wallpaper_item_spacing);
        int halfSpace = totalSpace / 2;

        recyclerView.setPadding(halfSpace, halfSpace, halfSpace, halfSpace);
        recyclerView.setClipToPadding(false);

        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                outRect.set(halfSpace, halfSpace, halfSpace, halfSpace);
            }
        });
        return root;
    }

    private void onWallpaperSelected(String name) {
        // 保存用户选择
        SharedPreferences prefs = requireContext().getSharedPreferences("weatherPrefs", Context.MODE_PRIVATE);
        prefs.edit().putString("wallpaperName", name).apply();
        // 通知天气页面更新背景
        FragmentActivity act = getActivity();
        if (act != null) {
            FragmentManager fm = act.getSupportFragmentManager();
            List<Fragment> frags = fm.getFragments();
            int success = 0;
            for (Fragment frag : frags) {
                if (frag instanceof WeatherFragment) {
                    ((WeatherFragment) frag).applyWallpaper();
                    success++;
                    continue;
                }
                if (frag instanceof AccountFragment) {
                    ((AccountFragment) frag).applyWallpaper();
                    success++;
                }
            }
            if (success == 2) {
                Toast.makeText(requireContext(), "所有Fragment壁纸更换成功！ ", Toast.LENGTH_SHORT).show();
            }
        }
    }
}